//
//  HttpRequest.m
//  IWaneHome
//
//  Created by hwd on 印度历1938/4/7.
//  Copyright © 印度历1938年 hwd_zlg. All rights reserved.
//
#define DemoServer @"http://api.hitalk.im/"
#import "HttpRequest.h"
#import "AFNetworking.h"
@interface HttpRequest ()
{
    OSSTask * putTask;
    NSString * result;
}
@end
@implementation HttpRequest

//get通用接口Dic
+(void)GetrequestUrl:(NSString *)url Dic:(NSDictionary *)dic completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}
//get通用接口Nsstring
+(void)GetrequestUrl:(NSString *)url  completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}
//post通用接口
+(void)PostrequestUrl:(NSString *)url Dic:(NSDictionary *)dic   completion:(void (^)(NSDictionary *responseDic))complete
{
//    NSString *urlStringUTF8 = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (jsonDic == NULL && responseObject !=NULL) {
            jsonDic =[NSMutableDictionary dictionaryWithDictionary:[self dictionaryWithJsonString:responseObject]];
        }
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}
//post通用接口2 
+(void)PostMuTableDicrequestUrl:(NSString *)url Dic:(NSMutableDictionary *)dic Controller:(UIViewController *)selfVC completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}
+ (NSDictionary *)dictionaryWithJsonString:(NSData *)data {
    if (data == nil) {
        return nil;
    }
    NSString *responseString = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
    
}
@end
